#pragma once

#include "Common/Singleton.h"
#include "Server/WorkerThread.h"
#include "TcpServerWrapper.h"
#include "LogicWorker.h"
#include "Cfg.h"

#define THREAD_COUNT	1
enum {
	Thread_Logic	=	0x01,
};

using namespace Network;

class DBConnectionMgr;

extern const char * s_HelpInfo;
extern UInt8 SERVER_TYPE;
extern UInt32 SERVER_ID;
extern std::string SERVER_NAME;

class GameManager;
class GameReportManager;
class Server
	: public Singleton<Server>
{
protected:
	Server();
	virtual ~Server();
	friend class Singleton<Server>;

public:
	bool init();
	void run();
	void shutdown();
	void unInit();
	void setAlive(bool = true);
	bool getAlive() const;
	bool parseCommandInfo(Int32, char **);
	std::string& getWorkDirectory();
	std::string  getFilePath(const std::string&);

public:
	inline Network::TcpServerWrapper * getTcpService() { return _tcpService;	}
	inline LogicWorker& getLogicWorker()
	{
		return (dynamic_cast<WorkerThread<LogicWorker>*>(_logicWorker))->Worker();
	}

	GameManager * getGameManager()	{ return _gameManager; }
	GameReportManager * getGameReportManager() { return _greportManager; }

protected:
	std::string			_workerDirectory;
	std::string			_serverName;
	BaseThread *		_logicWorker;
	TcpServerWrapper *	_tcpService;
	GameManager	*		_gameManager;
	GameReportManager * _greportManager;	
};

#define SERVER()	 Server::Instance()
#define NETWORK()	 SERVER().getTcpService()
#define LOGIC()	     SERVER().getLogicWorker()
#define PM()		 LOGIC().getPlayerManager()
#define GM()		 SERVER().getGameManager()
#define GRM()		 SERVER().getGameReportManager()

#define CURRENT_THREAD_ID() WorkerThread<WorkerRunner<> >::LocalWorker().GetThreadID()